<template>
	<view v-if="goodsInfo.goods_name" class="goods-detail-container">
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
			<swiper-item v-for="(item,i) in goodsInfo.pics" :key="i">
				<image :src="item.pics_big" @click="preview(i)"></image>
			</swiper-item>
		</swiper>

		<!-- 商品信息区域 -->
		<view class="goods-info-box">
			<!-- 商品价格 -->
			<view class="goods-price">¥{{goodsInfo.goods_price}}</view>
			<!-- 信息主题区域 -->
			<view class="goods-info-body">
				<!-- 商品名称 -->
				<view class="goods-name">{{goodsInfo.goods_name}}</view>
				<!-- 收藏 -->
				<view class="favi">
					<uni-icons type="star" size="18" color="gray"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<!-- 运费 -->
			<view class="yf">快递:免运费</view>
		</view>

		<!-- 商品详情信息 图文详情 -->
		<rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
		
		<!-- 商品导航组件 -->
		<view class="goods-nav">
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick" @buttonClick="buttonClick"></uni-goods-nav>
		</view>
	</view>
</template>

<script>
	import { mapState ,mapMutations, mapGetters} from 'vuex'
	export default {
		computed:{
			//...展开运算符
			//把m_cart模块中的cart数组映射到当前页面中,作为计算属性来使用
			//...mapState('模块的名称',['要映射的数据名称1','要映射的数据名称2'])
			...mapState('m_cart',[]),
			//把m_cart模块中名为total的getter映射到当前页面使用
			...mapGetters('m_cart',['total'])
			
		},
		
		watch:{
			
			//定义total监听器，指向一个配置对象
			total:{
				
				//handler属性用来定义侦听器的function函数
				handler(newValue){
					const findResult = this.options.find(x => x.text === '购物车')
					if(findResult){
						findResult.info = newValue
						console.log('count:' + findResult.info)
					}
				},
				//immediate属性用于侦听器是否在页面初次加载完毕后立即调用 
				immediate:true
			}
			
		},
		
		data() {
			return {
				goodsInfo: {},

				// 左侧按钮配置的信息
				options: [{
					icon: 'headphones',
					text: '客服'
				}, {
					icon: 'shop',
					text: '店铺'
				},{
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				// 右侧按钮配置的信息
				buttonGroup: [{
					text: '加入购物车',
					backgroundColor: '#ff0000',
					color: '#ffffff'
				}, {
					text: '立即购买',
					backgroundColor: '#ffa200',
					color: '#ffffff'
				}]
			};
		},
		

		onLoad(options) {
			//获取商品id
			const goods_id = options.goods_id
			console.log('options =' + JSON.stringify(options))
			//请求商品详情数据
			this.getGoodsInfoDetail(goods_id)
			
			// console.log('store:' + useStore())
		
		},

		methods: {
			...mapMutations('m_cart',["addToCart"]),
			async getGoodsInfoDetail(id) {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/detail', {
					goods_id: id
				})
				if (res.meta.status !== 200) return uni.$showMessage('')

				//使用字符串replace()方法，为img标签添加行内style样式，解决图片底部空白问题
				//webp格式的图片在ios设置上显示不好，这里替换成jpg就显示正常了
				res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g,
					'<img style="display:block;"').replace(/webp/g, 'jpg')
				this.goodsInfo = res.message
				// console.log("goods:" + JSON.stringify(this.goodsInfo))

			},

			preview(i) {
				//调用uni previewImage预览图片
				uni.previewImage({
					//预览时，默认现实图片索引
					current: i,
					//所有图片url的地址
					urls: this.goodsInfo.pics.map(x => x.pics_big)
				})
			},
			onClick(e){
				// console.log(e)
				if(e.content.text === '购物车'){
					uni.switchTab({
						url:'/pages/cart/cart'
					})
				}
			},
			
			buttonClick(e){
				if(e.content.text === '加入购物车'){
					const goods = {
						goods_id: this.goodsInfo.goods_id,
						goods_name:this.goodsInfo.goods_name,
						goods_price:this.goodsInfo.goods_price,
						goods_count:1,
						goods_small_logo:this.goodsInfo.goods_small_logo,
						goods_state:true     //勾选状态
					}
					this.addToCart(goods)
				}
			}
		}
	}
</script>

<style lang="scss">
	swiper {
		height: 750rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.goods-info-box {
		padding: 10px;
		padding-right: 0;

		.goods-price {
			color: #c00000;
			font-size: 16px;
		}

		.goods-info-body {
			display: flex;
			justify-content: space-between;
			margin-top: 10px;


			.goods-name {
				font-size: 13px;
				margin-right: 10px;
			}

			.favi {
				width: 120px;
				display: flex;
				flex-direction: column;
				font-size: 12px;
				align-items: center;
				border: 1px solid #efefef;
				color: gray;
			}

		}

		.yf {
			color: gray;
			margin-top: 10px;
			font-size: 13px;
		}
		
	}
	
	.goods-nav{
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}
	.goods-detail-container{
		padding-bottom: 50px;
	}
</style>
